package com.lg.erp.controller.maxuejiao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.SUser;
import com.lg.erp.entity.maxuejiao.TLaser;
import com.lg.erp.entity.maxuejiao.TPrinter;
import com.lg.erp.service.IUserService;
import com.lg.erp.service.maxuejiao.IJGDYService;
import com.lg.erp.service.maxuejiao.IJGMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
import java.util.UUID;

@RestController
public class JGMController {
    @Autowired
    private IJGMService ijgmService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IJGDYService ijgdyService;
    //分页1
    @RequestMapping("/jgms/{current}")
    public JsonResult selectAllLaser(@PathVariable Integer current){
        try {
            System.out.println("selectAllLaser");
            Page<TLaser> jgmPage = new Page<>(current,5);
            IPage<TLaser> jgmPages=ijgmService.selectJgmPage(jgmPage);
            return new JsonResult(1, "ok", jgmPages);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(0,"error",null);
    }
    //分页2
    @RequestMapping("/jgmsDy/{current}")
    public JsonResult selectAllLaserDy(@PathVariable Integer current){
        try {
            System.out.println("selectAllLaserDy");
            Page<TLaser> jgmPage = new Page<>(current,5);
            IPage<TLaser> jgmPages=ijgmService.selectJgmPageDy(jgmPage);
            return new JsonResult(1, "ok", jgmPages);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(0,"error",null);
    }
    @RequestMapping("/dyjs")
    public JsonResult selectAllDyj(){
        try {
            System.out.println("selectAllDyj");
            List<TPrinter> dyjList = ijgdyService.list();
            return new JsonResult(1, "ok",dyjList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(0,"error",null);
    }
    //批量删除
    @PostMapping("/jgmDel")
    public JsonResult deleteSome(@RequestBody List<TLaser> tableSelections){
        System.out.println("deleteSome");
        for (TLaser tableSelection : tableSelections) {
            System.out.println("删除id为"+tableSelection.getId()+"的数据");
            ijgmService.removeById(tableSelection.getId());
        }
        return new JsonResult(1,"ok",null);
    }
    //查询某个
    @GetMapping("/selectJgm/{orderId}")
    public JsonResult selectByOrderId(@PathVariable String orderId){
        System.out.println("selectByOrderId");
        System.out.println(orderId);
        TLaser laser = ijgmService.selectOneByOrderId(orderId);
        return new JsonResult(1, "ok", laser);
        /*return new JsonResult(1, "ok", null);*/
    }
    //获取companyId
    private String getCompanyId(){
        String companyId=null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof User){
            User user=(User) principal;
            SUser one = userService.getOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUsername, user.getUsername()));
            companyId = one.getCompanyId();
        }
        System.out.println("查询到的companyId:"+companyId);
        return companyId;
    }
    //批量生成
    @PostMapping("/jgmSc")
    public JsonResult scJgm(@RequestBody List<TLaser> tableSelections){
        System.out.println("scJgm");
        for (TLaser tableSelection : tableSelections) {
            System.out.println("id为"+tableSelection.getId()+"的数据");
            System.out.println("该数据的对应的order_id为"+tableSelection.getOrderId());
            //1.修改order表laser_code为1
            ijgmService.updateLCode(tableSelection.getOrderId());
            //给laser加入
            TLaser laser = new TLaser();
            laser.setId(UUID.randomUUID().toString().replace("-",""));
            laser.setLNo(tableSelection.getOrderId());
            laser.setLCode(UUID.randomUUID().toString().replace("-",""));
            laser.setLStatus("-1");
            laser.setLSerial(UUID.randomUUID().toString().replace("-",""));
            //获取companyId
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if(principal instanceof User){
                User user=(User) principal;
                SUser one = userService.getOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUsername, user.getUsername()));
                String companyId = one.getCompanyId();
                laser.setCompanyId(companyId);
            }
            System.out.println(laser.toString());
            ijgmService.save(laser);
        }
        return new JsonResult(1,"ok",null);
    }

    //批量分配
    @PostMapping("/zdFp")
    public JsonResult doZdFp(@RequestBody List<TLaser> tableSelections){
        System.out.println("================doZdFp=====================");
        List<TPrinter> dyjList = ijgdyService.list();
        System.out.println("打印机的总数为:"+dyjList.size());
        for (TLaser tableSelection : tableSelections) {
            System.out.println("orderId为"+tableSelection.getOrderId()+"的数据");
            int i2 = (int) Math.random() * (dyjList.size());
            Random random = new Random();
            int i = random.nextInt(dyjList.size());
            System.out.println("本次随机："+i);
            TPrinter tPrinter = dyjList.get(i);
            System.out.println("本次分配的打印机为:"+tPrinter.toString());
            String beAddPNum = tPrinter.getPNum();
            String beUpdateOrderId = tableSelection.getOrderId();
            //添加打印机(t_order/printer)
            ijgmService.addPrinterToOrder(beUpdateOrderId,beAddPNum);
            //添加打印机(t_laser/printer)
            ijgmService.addPrinterToLaser(beUpdateOrderId,beAddPNum);
            //改变打印状态(t_laser/l_status)
            ijgmService.changeLstatus(beUpdateOrderId);
        }
        return new JsonResult(1,"ok",null);
    }
    //批量手动分配
    @PostMapping("/sdFp/{pNum}")
    public JsonResult doSdFp(@RequestBody List<TLaser> tableSelections,@PathVariable String pNum){
        System.out.println("================doSdFp=====================");
        System.out.println(tableSelections.toString());
        System.out.println(pNum);
        for (TLaser tableSelection : tableSelections) {
            System.out.println("orderId为"+tableSelection.getOrderId()+"的数据");
            String beUpdateOrderId = tableSelection.getOrderId();
            //添加打印机(t_order/printer)
            ijgmService.addPrinterToOrder(beUpdateOrderId,pNum);
            //添加打印机(t_laser/printer)
            ijgmService.addPrinterToLaser(beUpdateOrderId,pNum);
            //改变打印状态(t_laser/l_status)
            ijgmService.changeLstatus(beUpdateOrderId);
        }
        return new JsonResult(1,"ok",null);
    }
}
